package cn.itsource.pethome.basic.controller;

import cn.itsource.pethome.org.service.IShopService;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@RestController
@RequestMapping("/mail")
public class SendMailController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private JavaMailSender javaMailSender;

    @GetMapping(value = "/{id}")
    @Transactional
    public AjaxResult sendEMail(@PathVariable("id") Long id){
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //发件人
            helper.setFrom("571787905@qq.com");
            //收件人
            //shopService.loadById(id).getEmployee().getEmail()
            helper.setTo(shopService.loadById(id).getEmployee().getEmail());
            //主题
            helper.setSubject("确认激活门店");
            //设置html类型的文本
            //"+id+"
            //http:/127.0.0.1:80/activation/"+id+"
            helper.setText("<h1>欢迎入驻宠物之家！</h1><br>" +
                    "点击<a href='http://127.0.0.1:80/activation/"+id+"'>这里</a>确认", true);
            javaMailSender.send(mimeMessage);
            return AjaxResult.me();
        } catch (MessagingException e) {
            e.printStackTrace();
            return new AjaxResult("网络延迟，请稍后再试");
        }
    }

}
